package yjj.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

/**
 * 非 spring 管理的类获取 bean 容器工具类
 */
@Component
public class SpringUtils {
    @Autowired
    private static ApplicationContext applicationContext;

    public static void setApplicationContext(ApplicationContext context) {
        applicationContext = context;
    }

    //通过提供想要获取的bean的class对象获取spring托管的bean，利用反射机制
    public static <T> T getBean(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }
}
